<?php
class Model_Tag_Tag extends Model_System {
	public $_name = "tag";
	protected $_sequence = true;
	public function getAll() {
		$result = $this->select();
		return $this->fetchAll($result)->toArray();
	}
	public function getItemId($id){
		$result = $this->select();
		$result->where('id = ?',$id);
		return $this->fetchRow($result)->toArray();
	}
	public function removeTag($id){
		$result = $this->delete('id = '.$id);
	}
	public function getItemByName($name){
		$result = $this->select();
		$result->where('name = ?',$name);
		return $this->fetchRow($result)->toArray();
	}
	public function getAllStartWith($char) {
		$result = $this->select();
		$result->where('name like ?','%'.$char.'%');
		$result->order("id DESC");
		return $this->fetchAll($result)->toArray();
	}
	static function getNamebyId($id)
	{
		$excute = new Model_Tag_Tag();
		$query = $excute->select();
		$query->where('id = ?',$id);
		$result = $excute->fetchRow($query)->toArray();
		return $result['name'];
	}
	public function getItemsByArticle($id){
		$result = $this->select();
		$result->where('article = ?',$id);
		return $this->fetchAll($result)->toArray();
	}
	public function getItemsByTag($id){
		$result = $this->select();
		$result->where('tag = ?',$id);
		return $this->fetchAll($result)->toArray();
	}
	public function checkName($name)
	{
	    $result = $this->select();
	    $result->where('name = ?',$name);
	    $item = $this->fetchAll($result)->toArray();
	    if(count($item)>0)
	        return true;
	    else
	        return false;
	}
	public function insertItem($data)
	{
		try {
		    $this->insert($data);
		} 
		catch (Exception $e) {
		    return $e->getMessage();
		}
		return "success";
		
	}
	public function updateItem($data,$id){
		try {
		    $this->update($data, "id = $id");
		} 
		catch (Exception $e) {
		    return $e->getMessage();
		}
		return "success";
	}
}